package com.ants.modules.utils;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.CreateResponse;
import co.elastic.clients.elasticsearch.core.DeleteResponse;
import co.elastic.clients.elasticsearch.core.UpdateResponse;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import co.elastic.clients.transport.endpoints.BooleanResponse;
import com.ants.modules.ArticleManage.dto.ArticleManageDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Slf4j
@Component
public class ElasticSearchUtils {

    @Autowired
    ElasticsearchClient client;

    /**
     * 创建索引
     *
     * @param index 索引名称
     * @throws IOException
     */
    public void createIndex(String index)  {
        //写法比RestHighLevelClient更加简洁
        try {
            CreateIndexResponse indexResponse = client.indices().create(c -> c.index(index));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取索引，判断其是否存在，存在则返回true
     *
     * @param index 索引名称
     * @return
     * @throws IOException
     */
    public Boolean existsIndex(String index){
        BooleanResponse booleanResponse = null;
        try {
            booleanResponse = client.indices().exists(e -> e.index(index));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return booleanResponse.value();
    }

    /**
     * 删除索引
     *
     * @param index 索引名称
     * @throws IOException
     */
    public void deleteIndex(String index) throws IOException {
        DeleteIndexResponse deleteIndexResponse = client.indices().delete(d -> d.index(index));
        System.out.println(deleteIndexResponse.acknowledged());
    }


    /**
     * 添加文档
     *
     * @param index    索引名称
     * @param id       ID
     * @param document 文档内容
     * @throws IOException
     */
    public void addDocument(String index, String id, ArticleManageDto document) {

        try {
            CreateResponse response = client.create(builder -> builder.index(index).id(id).document(document));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 判断文档是否存在
     *
     * @param index 索引名称
     * @param id    ID
     * @throws IOException
     */
    public void existsDocument(String index, String id) throws IOException {
        BooleanResponse indexResponse = client.exists(e -> e.index(index).id(id));
        System.out.println(indexResponse.value());
    }

    /**
     * 修改文档
     *
     * @param index    索引名称
     * @param id       ID
     * @param document 文档内容
     * @throws IOException
     */
    public void updateDocument(String index, String id, ArticleManageDto document) {
        try {
            UpdateResponse<ArticleManageDto> updateResponse = client.update(u -> u
                            .index(index)
                            .id(id)
                            .doc(document)
                    , ArticleManageDto.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除文档
     *
     * @param index 索引名称
     * @param id    ID
     * @throws IOException
     */
    public void deleteDocument(String index, String id) {
        try {
            DeleteResponse response = client.delete(builder -> builder.index(index).id(id));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
